﻿Public Class FrmKhachHang

    Private Sub FrmKhachHang_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadDuLieu()

    End Sub
    Dim provider As New DataProvider
    Dim flag As String = 0
    Dim sql As String

    Private Sub LoadDuLieu()
        Dim sql As String
        sql = "SELECT kh.MaKH, kh.TenKH, kh.DiaChiKH, kh.DienThoaiKH,nk.TenNhomKH FROM KhachHang kh, NhomKH nk WHERE kh.MaKH = nk.MaNhomKH"
        dtgkhachhang.DataSource = provider.ExecuteQuery(sql)
        Enable(False)
        btnluu.Enabled = False
        btnhuy.Enabled = False
        Enable(False)
        btnthem.Enabled = True
        btnsua.Enabled = True
        cbxnhomkh.Enabled = False


        LoadNhomKH()


    End Sub
    Private Sub LoadNhomKH()
        sql = "SELECT * FROM NhomKH nk"
        cbxnhomkh.DataSource = provider.ExecuteQuery(sql)
        cbxnhomkh.DisplayMember = "TenNhomKH"
        cbxnhomkh.ValueMember = "MaNhomKH"

    End Sub
    Private Sub Enable(ByVal ena As Boolean)
        txtdiachi.Enabled = ena
        txtmakh.Enabled = ena
        txtsdt.Enabled = ena
        txttenkh.Enabled = ena
        cbxnhomkh.Enabled = ena

    End Sub

    Private Sub dtgkhachhang_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dtgkhachhang.CellClick
        Try
            Dim row As Integer
            Dim sql As String
            Dim dt As New DataTable
            row = dtgkhachhang.CurrentCell.RowIndex
            txtmakh.Text = dtgkhachhang.Rows(row).Cells(1).Value
            txttenkh.Text = dtgkhachhang.Rows(row).Cells(2).Value
            txtdiachi.Text = dtgkhachhang.Rows(row).Cells(3).Value
            txtsdt.Text = dtgkhachhang.Rows(row).Cells(4).Value
            cbxnhomkh.Text = dtgkhachhang.Rows(row).Cells(0).Value
        Catch ex As Exception

        End Try
       

    End Sub

    Private Sub btnthem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnthem.Click
        Enable(True)
        flag = 1
        btnluu.Enabled = True
        btnhuy.Enabled = True
        btnsua.Enabled = False

        Enable(True)


    End Sub

    Private Sub btnhuy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnhuy.Click
        LoadDuLieu()
    End Sub

    Private Sub btnsua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsua.Click

        If txtmakh.Text = "" Then
            MessageBox.Show("Chọn 1 nhân viên để sửa ")
        Else
            flag = 2
            btnluu.Enabled = True
            btnhuy.Enabled = True
            btnthem.Enabled = False
            cbxnhomkh.Enabled = True
            Enable(True)
        End If
        
    End Sub

    Private Sub btnluu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnluu.Click
        Try
            If flag = 1 Then
                Dim manhomkh As String
                Dim makh As String
                Dim tenkh As String
                Dim diachi As String
                Dim sdt As String


                makh = txtmakh.Text.Trim
                tenkh = txttenkh.Text.Trim
                diachi = txtdiachi.Text.Trim
                sdt = txtsdt.Text.Trim
                manhomkh = cbxnhomkh.SelectedValue

                sql = "SELECT * FROM KhachHang kh WHERE kh.MaKH ='" + txtmakh.Text.Trim + "'"
                Dim dt As DataTable
                If dt.Rows.Count > 0 Then
                    MessageBox.Show("Mã khách hàng này tồn tại")
                    txtmakh.Focus()
                Else
                    sql = "INSERT INTO KhachHang(	MaKH,	TenKH,	DiaChiKH,	DienThoaiKH,	MaNhomKH)"
                    sql += "VALUES('" + makh + "',N'" + tenkh + "',N'" + diachi + "','" + sdt + "','" + manhomkh + "')"
                    provider.ExecuteNonQuery(sql)
                    MessageBox.Show("Thêm Khách Hàng Thành Công")

                End If

            ElseIf flag = 2 Then
                Dim manhomkh As String
                Dim makh As String
                Dim tenkh As String
                Dim diachi As String
                Dim sdt As String


                makh = txtmakh.Text.Trim
                tenkh = txttenkh.Text.Trim
                diachi = txtdiachi.Text.Trim
                sdt = txtsdt.Text.Trim
                manhomkh = cbxnhomkh.SelectedValue

                sql = "UPDATE KhachHang SET    TenKH =N'" + tenkh + "' ,    DiaChiKH = N'" + diachi + "' ,    DienThoaiKH ='" + sdt + "' ,    MaNhomKH ='" + manhomkh + "'  WHERE MaKH = '" + makh + "'"
                provider.ExecuteNonQuery(sql)
                MessageBox.Show("Chỉnh sửa thành công")

            End If
            LoadDuLieu()
        Catch ex As Exception

        End Try
    End Sub
End Class